<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2018/1/26
 * Time: 0:15
 */

namespace app\controllers;


use app\librarys\PayProController;
use app\models\File;
use app\models\MiniProgramsExt;

use yii\data\Pagination;

class MainController extends PayProController
{
    public $enableCsrfValidation = false;


    /**
     *
     * @return string
     */
    public function actionMiniProgram()
    {
        $request = \Yii::$app->request;

        $name = $request->get('name');

        $count = MiniProgramsExt::getMiniProgramCount($name);
        $pagination = new Pagination([
            'totalCount' => $count
        ]);
        $items = MiniProgramsExt::getMiniProgramList($pagination->page, $pagination->pageSize, $name);

        return $this->render('mini-program',
            [
                'items' => $items,
                'name' => $name,
                'pagination' => $pagination
            ]);
    }

    public function actionMiniProgramPartial()
    {
        $this->layout = false;
        $request = \Yii::$app->request;
        $id = $request->post('id');

        $item = MiniProgramsExt::getMiniProgramById($id);
        return $this->render('mini-program-partial',
            [
                'item' => $item
            ]
        );
    }

    public function actionMiniProgramSaveHandler()
    {
        $request = \Yii::$app->request;
        $id = $request->post('id');
        $name = $request->post('name');
        $raw_id = $request->post('raw_id');
        $app_id = $request->post('app_id');
        $app_secret = $request->post('app_secret');
        $type = $request->post('type');
        $begin_datetime = $request->post('begin_datetime');
        $end_datetime = $request->post('end_datetime');
        $is_enabled = $request->post('is_enabled');

        $result = MiniProgramsExt::saveMiniProgram($id, $name, $raw_id, $app_id, $app_secret, $type, $begin_datetime, $end_datetime, $is_enabled);
        return $this->renderJson(null, $result ? 0 : 1, $result ? '操作成功' : '操作失败');
    }

    public function actionMiniProgramDeleteHandler()
    {
        $request = \Yii::$app->request;
        $id = $request->post('id');

        $result = MiniProgramsExt::deleteMiniProgram($id);
        return $this->renderJson(null, $result ? 0 : 1, $result ? '操作成功' : '操作失败');
    }


    public function actionUploadImageHandler()
    {
        $request = \Yii::$app->request;
        $img_cord = json_decode($request->post('img_cord'));

        if (!empty($_FILES['uploadImg']['name'])) {
            if ($_FILES['uploadImg']['error'] == 0) {
                if ($img_cord) {
                    $path = File::CutImage($_FILES['uploadImg'], $img_cord->x, $img_cord->y, $img_cord->w, $img_cord->h, $img_cord->size_w, $img_cord->size_h);
                } else {
                    return $this->renderJson('', 1, '图片需要裁剪');
                }
                if (!$path) {
                    return $this->renderJson('', 1, '图片格式错误');
                }
            }
        }
        $data = [
            'path' => $path
        ];
        return $this->renderJson($data);
    }
}